package com.qf.smartplatform.security.handlers;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.smartplatform.cache.RoleCache;
import com.qf.smartplatform.dto.Result;
import com.qf.smartplatform.mapper.RoleMapper;
import com.qf.smartplatform.pojo.BaseUser;
import com.qf.smartplatform.pojo.Menu;
import com.qf.smartplatform.pojo.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * projectName:day12_27_Test
 * author:guoLei
 * time:2022/1/9 17:40
 * description:
 */
@Component
public class MyAuthenSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {

    private ObjectMapper objectMapper;

    private RoleMapper roleMapper;

    private RoleCache roleCache;

    private ApplicationContext context;

    @Autowired
    public void setContext(ApplicationContext context) {
        this.context = context;
    }
    @Autowired
    public void setRoleCache(RoleCache roleCache) {
        this.roleCache = roleCache;
    }

    @Autowired
    public void setRoleMapper(RoleMapper roleMapper) {
        this.roleMapper = roleMapper;
    }
//
    @Autowired
    public void setObjectMapper(ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
    }

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        BaseUser principal = (BaseUser) authentication.getPrincipal();
        Long userId = principal.getUserId();
        List<SimpleGrantedAuthority>authorityList = new ArrayList<>();
        List<Long> roleIds = roleMapper.selectRoleIdByUserId(userId);
        roleIds.stream().forEach(roleId->{
            Role role = roleCache.getValue(roleId);
            List<Menu> menuList = role.getMenuList();
            authorityList.addAll(menuList.parallelStream().map(Menu::getPerms).map(SimpleGrantedAuthority::new).collect(Collectors.toList()));
        });
        SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(principal,authentication.getCredentials(),authorityList));
        response.setContentType("application/json;charset=utf-8");
        Result result=Result.setOK();
        response.getWriter().write(objectMapper.writeValueAsString(result));


    }
}
